import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  // 定义的就是仓库中要操作的数据
  state: {
    count: 100,
    userInfo: {}
  },
  getters: {
    getCount (state) {
      return state.count
    },
    getUserInfo (state) {
      return state.userInfo
    }
  },
  // 同步更改仓库中的值，触发mutation要通过 $store.commit('函数的名字')
  mutations: {
    add (state, num) {
      state.count += num
    },
    minus (state, num) {
      state.count -= num
    },
    setUserInfo (state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    asyncMinus (context, payload) {
      setTimeout(() => {
        context.commit('minus', payload)
      }, 2000)
    }
  }
})

export default store
